<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/5/26
 * Time: 16:35
 */

namespace Home\Service;


use Common\Common\Service;
use Home\Component\ApplyComponent;
use Home\Component\UserComponent;
use Home\Exception\HomeException;
use Home\Request\Apply\AddNumRequest;
use Home\Response\AddNumResponse;

class AddNumService implements Service
{
    public function run(AddNumRequest $request = null)
    {
        $apply_id = $request->getApplyId();
        $user_id = $request->getUserId();
        $flag = $request->getFlag();
        $component = new ApplyComponent();
        $response = new AddNumResponse();
        try {
            $data = $component->addNum($apply_id);
            if ($flag == false) {
                if ($data == 0) {
                    $response->setItIsNull();
                } else {
                    $component = new UserComponent();
                    $time = date('Y-m-d H:i:s');
                    $component->saveTime($time, $user_id);
                    $component->incDayNum($user_id);
                }
            }

        } catch (HomeException $e) {
            throw new HomeException($e->getMessage(), $e->getCode());
        }
        return $response;
    }
}